Setup

# install.packages("keras")
# install.packages("lime")
# system('add-apt-repository -y ppa:cran/imagemagick')
# system('apt-get update')
# system("apt-get install libmagick++-dev")
# install.packages("magick")
# install.packages('abind')
library(keras)
library(lime)   
library(magick) 
## Linking to ImageMagick 6.9.10.23
## Enabled features: fontconfig, freetype, fftw, lcms, pango, webp, x11
## Disabled features: cairo, ghostscript, heic, raw, rsvg
## Using 4 threads
library(ggplot2)
library(abind)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:lime':
## 
##     explain
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

utils

to_df <- function(explanation){
  explanation_df <- data.frame(model_type = explanation$model_type,
                              case = explanation$case,
                              label = explanation$label,
                              label_prob = explanation$label_prob,
                              model_r2 = explanation$model_r2,
                              model_intercept = explanation$model_intercept,
                              model_prediction = explanation$model_prediction,
                              feature = explanation$feature,
                              feature_value = NA,
                              feature_weight = explanation$feature_weight,
                              feature_desc = explanation$feature_desc,
                              data = NA,
                              prediction = NA,
                              stringsAsFactors = FALSE)

  ## this will coerce superpixel_list and bitmap_list to list, but still works for plotting
  for (i in 1:nrow(explanation_df)){
    explanation_df$feature_value[i] <- explanation$feature_value[i]
    explanation_df$data[i] <- explanation$data[i]
    explanation_df$prediction[i] <- explanation$prediction[i]
  }
  return(explanation_df)
}

image_prep <- function(x) {
  arrays <- lapply(x, function(path) {
    img <- image_load(path, target_size = c(224,224))
    x <- image_to_array(img)
    x <- array_reshape(x, c(1, dim(x)))
    x <- imagenet_preprocess_input(x)
  })
  do.call(abind::abind, c(arrays, list(along = 1)))
}

Modelos

model_vgg16 <- application_vgg16(weights = "imagenet", include_top = TRUE)
## Loaded Tensorflow version 2.10.0
model_resnet50 <- application_resnet50(weights = "imagenet", include_top = TRUE)
model_labels <- readRDS(system.file('extdata', 'imagenet_labels.rds', package = 'lime'))

Explainers

explainer_vgg16 <- lime(c('flower_0269.jpg'), as_classifier(model_vgg16, model_labels), image_prep)

explainer_resnet50 <- lime(c('flower_0269.jpg'), as_classifier(model_resnet50, model_labels), image_prep)

object explanation

expl_flower_0269 <- lime::explain(c('flower_0269.jpg'), explainer_vgg16, 
                       n_labels = 1, 
                       n_features = 10,
                       n_superpixels = 50, weight = 1,
                       n_permutations = 100,
                       background = "white"
                       )
#plot_image_explanation(expl_flower_0269)
#expl_flower_0269[1,]
class(expl_flower_0269)
## [1] "tbl_df"     "tbl"        "data.frame"
expl_template <- .load_image_example()
class(expl_template)
## [1] "data.frame"
expl_template
##        model_type        case                    label label_prob  model_r2
## 1  classification produce.png               strawberry  0.3546817 0.4310426
## 2  classification produce.png               strawberry  0.3546817 0.4310426
## 3  classification produce.png               strawberry  0.3546817 0.4310426
## 4  classification produce.png               strawberry  0.3546817 0.4310426
## 5  classification produce.png               strawberry  0.3546817 0.4310426
## 6  classification produce.png               strawberry  0.3546817 0.4310426
## 7  classification produce.png               strawberry  0.3546817 0.4310426
## 8  classification produce.png               strawberry  0.3546817 0.4310426
## 9  classification produce.png               strawberry  0.3546817 0.4310426
## 10 classification produce.png               strawberry  0.3546817 0.4310426
## 11 classification produce.png candle, taper, wax light  0.1550600 0.3906374
## 12 classification produce.png candle, taper, wax light  0.1550600 0.3906374
## 13 classification produce.png candle, taper, wax light  0.1550600 0.3906374
## 14 classification produce.png candle, taper, wax light  0.1550600 0.3906374
## 15 classification produce.png candle, taper, wax light  0.1550600 0.3906374
## 16 classification produce.png candle, taper, wax light  0.1550600 0.3906374
## 17 classification produce.png candle, taper, wax light  0.1550600 0.3906374
## 18 classification produce.png candle, taper, wax light  0.1550600 0.3906374
## 19 classification produce.png candle, taper, wax light  0.1550600 0.3906374
## 20 classification produce.png candle, taper, wax light  0.1550600 0.3906374
## 21 classification produce.png             Granny Smith  0.1225764 0.1457302
## 22 classification produce.png             Granny Smith  0.1225764 0.1457302
## 23 classification produce.png             Granny Smith  0.1225764 0.1457302
## 24 classification produce.png             Granny Smith  0.1225764 0.1457302
## 25 classification produce.png             Granny Smith  0.1225764 0.1457302
## 26 classification produce.png             Granny Smith  0.1225764 0.1457302
## 27 classification produce.png             Granny Smith  0.1225764 0.1457302
## 28 classification produce.png             Granny Smith  0.1225764 0.1457302
## 29 classification produce.png             Granny Smith  0.1225764 0.1457302
## 30 classification produce.png             Granny Smith  0.1225764 0.1457302
##    model_intercept model_prediction feature     feature_value feature_weight
## 1      0.063138829      0.365205724      50 2994px superpixel   0.1394982102
## 2      0.063138829      0.365205724      42 1492px superpixel   0.1014987675
## 3      0.063138829      0.365205724      56 3097px superpixel   0.0816106280
## 4      0.063138829      0.365205724      43 2563px superpixel   0.0768424380
## 5      0.063138829      0.365205724      38 1815px superpixel  -0.0533579953
## 6      0.063138829      0.365205724      54 3166px superpixel   0.0441655821
## 7      0.063138829      0.365205724      41 2254px superpixel  -0.0473633081
## 8      0.063138829      0.365205724      26 1252px superpixel  -0.0436434659
## 9      0.063138829      0.365205724      44  928px superpixel   0.0402079540
## 10     0.063138829      0.365205724      20 2744px superpixel  -0.0373919153
## 11     0.202270823      0.510950719      20 2744px superpixel   0.1572028999
## 12     0.202270823      0.510950719      38 1815px superpixel   0.1361720436
## 13     0.202270823      0.510950719       9 3111px superpixel  -0.1143510264
## 14     0.202270823      0.510950719      41 2254px superpixel   0.1189565730
## 15     0.202270823      0.510950719      36 1156px superpixel   0.1213101689
## 16     0.202270823      0.510950719       8  521px superpixel  -0.1072294739
## 17     0.202270823      0.510950719      18 2093px superpixel   0.0869311809
## 18     0.202270823      0.510950719      39 3041px superpixel   0.0607053823
## 19     0.202270823      0.510950719       2 1538px superpixel  -0.0774141241
## 20     0.202270823      0.510950719      42 1492px superpixel  -0.0736037281
## 21    -0.002948256      0.007209071      52 2633px superpixel   0.0017020169
## 22    -0.002948256      0.007209071      51 3010px superpixel   0.0014061881
## 23    -0.002948256      0.007209071       9 3111px superpixel   0.0014092894
## 24    -0.002948256      0.007209071      59  926px superpixel   0.0013329257
## 25    -0.002948256      0.007209071      43 2563px superpixel  -0.0013316915
## 26    -0.002948256      0.007209071      45 1040px superpixel   0.0011713706
## 27    -0.002948256      0.007209071      21 1220px superpixel   0.0013799368
## 28    -0.002948256      0.007209071      16 1893px superpixel   0.0010632344
## 29    -0.002948256      0.007209071      39 3041px superpixel   0.0010874609
## 30    -0.002948256      0.007209071       8  521px superpixel   0.0009365956
##            feature_desc                     data
## 1  [206-269], [250-323] 4 channel 323x311 bitmap
## 2  [167-221], [260-319] 4 channel 323x311 bitmap
## 3  [252-311], [131-202] 4 channel 323x311 bitmap
## 4  [172-248], [232-278] 4 channel 323x311 bitmap
## 5  [163-218], [106-174] 4 channel 323x311 bitmap
## 6  [237-311], [171-242] 4 channel 323x311 bitmap
## 7   [165-224], [67-134] 4 channel 323x311 bitmap
## 8   [108-146], [57-120] 4 channel 323x311 bitmap
## 9  [185-245], [200-247] 4 channel 323x311 bitmap
## 10     [64-131], [1-62] 4 channel 323x311 bitmap
## 11     [64-131], [1-62] 4 channel 323x311 bitmap
## 12 [163-218], [106-174] 4 channel 323x311 bitmap
## 13    [19-70], [49-160] 4 channel 323x311 bitmap
## 14  [165-224], [67-134] 4 channel 323x311 bitmap
## 15  [146-179], [97-163] 4 channel 323x311 bitmap
## 16   [15-52], [172-200] 4 channel 323x311 bitmap
## 17    [52-128], [35-97] 4 channel 323x311 bitmap
## 18   [164-219], [17-86] 4 channel 323x311 bitmap
## 19      [1-44], [37-78] 4 channel 323x311 bitmap
## 20 [167-221], [260-319] 4 channel 323x311 bitmap
## 21   [216-279], [27-95] 4 channel 323x311 bitmap
## 22  [209-283], [77-143] 4 channel 323x311 bitmap
## 23    [19-70], [49-160] 4 channel 323x311 bitmap
## 24  [272-292], [43-114] 4 channel 323x311 bitmap
## 25 [172-248], [232-278] 4 channel 323x311 bitmap
## 26   [186-262], [11-43] 4 channel 323x311 bitmap
## 27  [64-136], [119-175] 4 channel 323x311 bitmap
## 28  [49-103], [135-192] 4 channel 323x311 bitmap
## 29   [164-219], [17-86] 4 channel 323x311 bitmap
## 30   [15-52], [172-200] 4 channel 323x311 bitmap
colnames(expl_flower_0269)
##  [1] "model_type"       "case"             "label"            "label_prob"      
##  [5] "model_r2"         "model_intercept"  "model_prediction" "feature"         
##  [9] "feature_value"    "feature_weight"   "feature_desc"     "data"            
## [13] "prediction"
explanations <- lime::explain(c('flower_0269.jpg','motorbike_0032.jpg'), explainer_vgg16, 
                       n_labels = 5, 
                       n_features = 10,
                       n_superpixels = 50, weight = 1,
                       n_permutations = 200,
                       background = "white"
                       )

to_df(explanations) %>%
  ggplot(aes(x = feature_weight)) +
    facet_wrap(~ case + label, scales = "free") +
    geom_density()

Resultados

Moto

#Imagen original a clasificar
print(image_read('motorbike_0032.jpg'))
## # A tibble: 1 × 7
##   format width height colorspace matte filesize density
##   <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
## 1 JPEG     200    136 sRGB       FALSE    20663 72x72

#Visualizamos los superpixels obtenidos de la imagen
plot_superpixels('motorbike_0032.jpg', n_superpixels = 50, weight = 5)

VGG16

#Clasificacion de la imagen segun el modelo VGG16
res_vgg16_motorbike <- predict(model_vgg16, image_prep(c('motorbike_0032.jpg')))
imagenet_decode_predictions(res_vgg16_motorbike)
## [[1]]
##   class_name class_description      score
## 1  n03127747      crash_helmet 0.47350988
## 2  n03785016             moped 0.27609763
## 3  n03208938        disk_brake 0.11000329
## 4  n03792782     mountain_bike 0.09415597
## 5  n03791053     motor_scooter 0.02573411
#Explain
explanation <- lime::explain(c('motorbike_0032.jpg'), explainer_vgg16, 
                       n_labels = 1, 
                       n_features = 100,
                       n_superpixels = 100, weight = 10,
                       n_permutations = 500,
                       background = "white")
plot_image_explanation(
to_df(explanation),
which = 1,
threshold = 0.02,
show_negative = TRUE,
display = "outline", #"outline"
fill_alpha = 0.3,
outline_col = c("blue", "red"),
block_col = "grey"
)

plot_features(explanation, ncol = 3, cases = NULL)

explanation %>%
  ggplot(aes(x = feature_weight)) +
    facet_wrap(~ case, scales = "free") +
    geom_density()

plot_image_explanation(
  to_df(explanation),
  which = 1,
  threshold = 0.02,
  show_negative = FALSE,
  display = "block", #"outline"
  fill_alpha = 0.3,
  outline_col = c("blue", "red"),
  block_col = "grey"
  )

RESNET50

explanation <- lime::explain(c('motorbike_0032.jpg'), explainer_resnet50, 
                       n_labels = 1, 
                       n_features = 50,
                       n_superpixels = 50, weight = 10,
                       n_permutations = 500,
                       background = "white")
plot_image_explanation(
  to_df(explanation),
  which = 1,
  threshold = 0.02,
  show_negative = TRUE,
  display = "outline", #"outline"
  fill_alpha = 0.3,
  outline_col = c("blue", "red"),
  block_col = "grey"
  )

plot_features(explanation, ncol = 3, cases = NULL)

plot_image_explanation(
to_df(explanation),
which = 1,
threshold = 0.02,
show_negative = FALSE,
display = "block", #"outline"
fill_alpha = 0.3,
outline_col = c("blue", "red"),
block_col = "grey"
)

Flor

#Imagen original a clasificar
plot(image_read('flower_0269.jpg'))

#Visualizamos los superpixels obtenidos de la imagen
plot_superpixels('flower_0269.jpg', n_superpixels = 50, weight = 10)

VGG16

res_vgg16_flower <- predict(model_vgg16, image_prep(c('flower_0269.jpg')))
imagenet_decode_predictions(res_vgg16_flower)
## [[1]]
##   class_name class_description      score
## 1  n03991062               pot 0.22525069
## 2  n07720875       bell_pepper 0.09684577
## 3  n02206856               bee 0.06948115
## 4  n03457902        greenhouse 0.05719404
## 5  n03930313      picket_fence 0.04914082
explanation <- lime::explain(c('flower_0269.jpg'), explainer_vgg16, 
                       n_labels = 1, 
                       n_features = 50,
                       n_superpixels = 50, weight = 10,
                       n_permutations = 500,
                       background = "white")
plot_image_explanation(
  to_df(explanation),
  which = 1,
  threshold = 0.02,
  show_negative = TRUE,
  display = "outline", #"outline"
  fill_alpha = 0.3,
  outline_col = c("blue", "red"),
  block_col = "grey"
  )

plot_features(explanation, ncol = 3, cases = NULL)

explanation %>%
  ggplot(aes(x = feature_weight)) +
    facet_wrap(~ case, scales = "free") +
    geom_density()

plot_image_explanation(
  to_df(explanation),
  which = 1,
  threshold = 0.02,
  show_negative = FALSE,
  display = "block", #"outline"
  fill_alpha = 0.3,
  outline_col = c("blue", "red"),
  block_col = "grey"
  )

RESNET50

res_resnet50_flower <- predict(model_resnet50, image_prep(c('flower_0269.jpg')))
imagenet_decode_predictions(res_resnet50_flower)
## [[1]]
##   class_name class_description      score
## 1  n03991062               pot 0.40786061
## 2  n03457902        greenhouse 0.15969767
## 3  n07714571      head_cabbage 0.07762894
## 4  n07717410      acorn_squash 0.06562954
## 5  n07718472          cucumber 0.05744600
explanation <- lime::explain(c('flower_0269.jpg'), explainer_resnet50, 
                       n_labels = 1, 
                       n_features = 50,
                       n_superpixels = 50, weight = 10,
                       n_permutations = 500,
                       background = "white")
plot_image_explanation(
  to_df(explanation),
  which = 1,
  threshold = 0.02,
  show_negative = TRUE,
  display = "outline", #"outline"
  fill_alpha = 0.3,
  outline_col = c("blue", "red"),
  block_col = "grey"
  )

plot_image_explanation(
  to_df(explanation),
  which = 1,
  threshold = 0.02,
  show_negative = FALSE,
  display = "block", #"outline"
  fill_alpha = 0.3,
  outline_col = c("blue", "red"),
  block_col = "grey"
  )

Perro

#Imagen original a clasificar
plot(image_read('dog_0170.jpg'))

#Visualizamos los superpixels obtenidos de la imagen
plot_superpixels('dog_0170.jpg', n_superpixels = 50, weight = 10)

#### VGG16

#Clasificacion de la imagen segun el modelo VGG16
res_vgg16_dog <- predict(model_vgg16, image_prep(c('dog_0170.jpg')))
imagenet_decode_predictions(res_vgg16_dog)
## [[1]]
##   class_name              class_description       score
## 1  n02093428 American_Staffordshire_terrier 0.630665898
## 2  n02093256      Staffordshire_bullterrier 0.280850619
## 3  n02110806                        basenji 0.064215504
## 4  n02108915                 French_bulldog 0.006692672
## 5  n02108089                          boxer 0.005174202
explanation <- lime::explain(c('dog_0170.jpg'), explainer_vgg16, 
                       n_labels = 1, 
                       n_features = 50,
                       n_superpixels = 50, weight = 10,
                       n_permutations = 500,
                       background = "white")
plot_image_explanation(
  to_df(explanation),
  which = 1,
  threshold = 0.02,
  show_negative = TRUE,
  display = "outline", #"outline"
  fill_alpha = 0.3,
  outline_col = c("blue", "red"),
  block_col = "grey"
  )

RESNET50

#Clasificacion de la imagen segun el modelo VGG16
res_resnet50_dog <- predict(model_resnet50, image_prep(c('dog_0170.jpg')))
imagenet_decode_predictions(res_resnet50_dog)
## [[1]]
##   class_name              class_description       score
## 1  n02093256      Staffordshire_bullterrier 0.572505355
## 2  n02093428 American_Staffordshire_terrier 0.231044978
## 3  n02091032              Italian_greyhound 0.142449960
## 4  n02110806                        basenji 0.042962722
## 5  n02091134                        whippet 0.005982167
explanation <- lime::explain(c('dog_0170.jpg'), explainer_resnet50, 
                       n_labels = 1, 
                       n_features = 50,
                       n_superpixels = 50, weight = 10,
                       n_permutations = 500,
                       background = "white")
plot_image_explanation(
  to_df(explanation),
  which = 1,
  threshold = 0.02,
  show_negative = TRUE,
  display = "outline", #"outline"
  fill_alpha = 0.3,
  outline_col = c("blue", "red"),
  block_col = "grey"
  )

Evaluación

expl_flower_0269_vgg16 <- lime::explain(c('flower_0269.jpg'), explainer_vgg16, 
                       n_labels = 1, 
                       n_features = 10,
                       n_superpixels = 50, weight = 1,
                       n_permutations = 200,
                       background = "white"
                       )

expl_motorbike_0032_vgg16 <- lime::explain(c('motorbike_0032.jpg'), explainer_vgg16, 
                       n_labels = 1, 
                       n_features = 10,
                       n_superpixels = 50, weight = 1,
                       n_permutations = 200,
                       background = "white"
                       )

expl_dog_0170_vgg16 <- lime::explain(c('dog_0170.jpg'), explainer_vgg16, 
                       n_labels = 1, 
                       n_features = 10,
                       n_superpixels = 50, weight = 1,
                       n_permutations = 200,
                       background = "white"
                       )

expl_flower_0269_resnet50 <- lime::explain(c('flower_0269.jpg'), explainer_resnet50, 
                       n_labels = 1, 
                       n_features = 10,
                       n_superpixels = 50, weight = 1,
                       n_permutations = 200,
                       background = "white"
                       )

expl_motorbike_0032_resnet50 <- lime::explain(c('motorbike_0032.jpg'), explainer_resnet50, 
                      n_labels = 1, 
                      n_features = 10,
                      n_superpixels = 50, weight = 1,
                      n_permutations = 200,
                      background = "white"
                      )

expl_dog_0170_resnet50 <- lime::explain(c('dog_0170.jpg'), explainer_resnet50, 
                       n_labels = 1, 
                       n_features = 10,
                       n_superpixels = 50, weight = 1,
                       n_permutations = 200,
                       background = "white"
                       )

explanations = c(expl_flower_0269_vgg16, expl_flower_0269_resnet50, expl_motorbike_0032_vgg16, expl_motorbike_0032_resnet50, expl_dog_0170_vgg16, expl_dog_0170_resnet50)
explanations_vgg16 <- c(expl_flower_0269_vgg16, expl_motorbike_0032_vgg16, expl_dog_0170_vgg16)
explanations_resnet50 <- c(expl_flower_0269_resnet50, expl_motorbike_0032_resnet50, expl_dog_0170_resnet50)
for (explanation in explanations){
  p <- to_df(explanations) %>%
    ggplot(aes(x = feature_weight)) +
      facet_wrap(~ case + label, scales = "free") +
      geom_density()
  print(p)
}

plot_image_explanation(
  to_df(expl_motorbike_0032_vgg16),
  which = 1,
  threshold = 0.0,
  display = "outline", #"outline"
  fill_alpha = 0.3,
  outline_col = c("blue", "red")
  )

plot_image_explanation(to_df(expl_motorbike_0032_vgg16), display='block', block_col = "white", threshold=0.02)

plot_image_explanation(to_df(expl_motorbike_0032_vgg16), display='block', block_col = "white", threshold=0.015)

plot_image_explanation(to_df(expl_motorbike_0032_vgg16), display='block', block_col = "white", threshold=0.01)

plot_image_explanation(to_df(expl_flower_0269_vgg16), display='block', block_col = "white", threshold=0.03)

plot_image_explanation(to_df(expl_flower_0269_vgg16), display='block', block_col = "white", threshold=0.02)

plot_image_explanation(to_df(expl_flower_0269_vgg16), display='block', block_col = "white", threshold=0.015)

plot_image_explanation(to_df(expl_flower_0269_vgg16), display='block', block_col = "white", threshold=0.005)

plot_features(to_df(expl_dog_0170_vgg16))

plot_image_explanation(to_df(expl_dog_0170_vgg16), display='block', block_col = "white", threshold=0.03)

plot_image_explanation(to_df(expl_dog_0170_vgg16), display='block', block_col = "white", threshold=0.02)

plot_image_explanation(to_df(expl_dog_0170_vgg16), display='block', block_col = "white", threshold=0.015)

plot_image_explanation(to_df(expl_dog_0170_resnet50), display='block', block_col = "white", threshold=0.01)

plot_image_explanation(to_df(expl_dog_0170_resnet50), display='block', block_col = "white", threshold=0.005)

plot_image_explanation(to_df(expl_dog_0170_resnet50), display='outline', outline_col = c("green","red"), threshold=0.005)

plot_image_explanation(to_df(expl_motorbike_0032_resnet50), display='block', block_col = "white", threshold=0.02)

plot_image_explanation(to_df(expl_motorbike_0032_resnet50), display='block', block_col = "white", threshold=0.015)

plot_image_explanation(to_df(expl_motorbike_0032_resnet50), display='block', block_col = "white", threshold=0.01)

plot_image_explanation(to_df(expl_flower_0269_resnet50), display='block', block_col = "white", threshold=0.06)

plot_image_explanation(to_df(expl_flower_0269_resnet50), display='block', block_col = "white", threshold=0.02)

plot_image_explanation(to_df(expl_flower_0269_resnet50), display='block', block_col = "white", threshold=0.015)

plot_image_explanation(to_df(expl_flower_0269_resnet50), display='block', block_col = "white", threshold=0.01)

plot_image_explanation(to_df(expl_flower_0269_resnet50), display='block', block_col = "white", threshold=0.00005)

plot_image_explanation(to_df(expl_dog_0170_resnet50), display='block', block_col = "white", threshold=0.04)

plot_image_explanation(to_df(expl_dog_0170_resnet50), display='block', block_col = "white", threshold=0.02)

plot_image_explanation(to_df(expl_dog_0170_resnet50), display='block', block_col = "white", threshold=0.015)

plot_image_explanation(to_df(expl_dog_0170_resnet50), display='block', block_col = "white", threshold=0.01)

plot_image_explanation(to_df(expl_dog_0170_resnet50), display='block', block_col = "white", threshold=0.005)

expls_resnet <- c('flor_resnet_00005.png','flor_resnet_001.png','flor_resnet_0015.png','flor_resnet_002.png','flor_resnet_006.png',
                'moto_resnet_001.png','moto_resnet_0015.png','moto_resnet_002.png',
                'perro_resnet_004.png','perro_resnet_0005.png','perro_resnet_001.png','perro_resnet_0015.png','perro_resnet_002.png')

for (expl in expls_resnet){
  res <- predict(model_resnet50, image_prep(expl))
  pred <- imagenet_decode_predictions(res)
  print(expl)
  print(pred)
}
## [1] "flor_resnet_00005.png"
## [[1]]
##   class_name class_description      score
## 1  n03443371            goblet 0.55889320
## 2  n04522168              vase 0.18716650
## 3  n03888257         parachute 0.04991456
## 4  n11939491             daisy 0.04841070
## 5  n06359193          web_site 0.01254371
## 
## [1] "flor_resnet_001.png"
## [[1]]
##   class_name class_description      score
## 1  n11939491             daisy 0.28510550
## 2  n04522168              vase 0.24932671
## 3  n03443371            goblet 0.19348635
## 4  n03888257         parachute 0.05217402
## 5  n02280649 cabbage_butterfly 0.03128443
## 
## [1] "flor_resnet_0015.png"
## [[1]]
##   class_name class_description      score
## 1  n04522168              vase 0.39725581
## 2  n11939491             daisy 0.19774289
## 3  n03443371            goblet 0.17575277
## 4  n03888257         parachute 0.06895842
## 5  n03991062               pot 0.01821515
## 
## [1] "flor_resnet_002.png"
## [[1]]
##   class_name class_description      score
## 1  n04522168              vase 0.42114443
## 2  n11939491             daisy 0.23756297
## 3  n03888257         parachute 0.04077648
## 4  n02280649 cabbage_butterfly 0.03015097
## 5  n03991062               pot 0.02884009
## 
## [1] "flor_resnet_006.png"
## [[1]]
##   class_name class_description      score
## 1  n04522168              vase 0.30719513
## 2  n03443371            goblet 0.14172523
## 3  n03485794      handkerchief 0.09345738
## 4  n03888257         parachute 0.04718548
## 5  n06359193          web_site 0.03323954
## 
## [1] "moto_resnet_001.png"
## [[1]]
##   class_name class_description      score
## 1  n03792782     mountain_bike 0.10805873
## 2  n03126707             crane 0.09858414
## 3  n02708093      analog_clock 0.09082291
## 4  n03127747      crash_helmet 0.06038012
## 5  n04552348          warplane 0.03419653
## 
## [1] "moto_resnet_0015.png"
## [[1]]
##   class_name class_description      score
## 1  n03127747      crash_helmet 0.07377856
## 2  n03792782     mountain_bike 0.07167485
## 3  n06359193          web_site 0.06592266
## 4  n03888257         parachute 0.05935331
## 5  n03126707             crane 0.05655099
## 
## [1] "moto_resnet_002.png"
## [[1]]
##   class_name class_description       score
## 1  n02708093      analog_clock 0.780441761
## 2  n02794156         barometer 0.066270776
## 3  n04548280        wall_clock 0.040770110
## 4  n03000684         chain_saw 0.012807607
## 5  n03447721              gong 0.008527793
## 
## [1] "perro_resnet_004.png"
## [[1]]
##   class_name              class_description       score
## 1  n02093428 American_Staffordshire_terrier 0.792773306
## 2  n02093256      Staffordshire_bullterrier 0.187786549
## 3  n02108089                          boxer 0.013415515
## 4  n02108422                   bull_mastiff 0.002793905
## 5  n02087394            Rhodesian_ridgeback 0.001912791
## 
## [1] "perro_resnet_0005.png"
## [[1]]
##   class_name              class_description        score
## 1  n02093428 American_Staffordshire_terrier 0.8295236230
## 2  n02093256      Staffordshire_bullterrier 0.1514441073
## 3  n02108089                          boxer 0.0103839682
## 4  n02087394            Rhodesian_ridgeback 0.0043983711
## 5  n02108422                   bull_mastiff 0.0006879895
## 
## [1] "perro_resnet_001.png"
## [[1]]
##   class_name              class_description       score
## 1  n02093428 American_Staffordshire_terrier 0.778642774
## 2  n02093256      Staffordshire_bullterrier 0.198025256
## 3  n02087394            Rhodesian_ridgeback 0.011567560
## 4  n02108089                          boxer 0.007423394
## 5  n02108422                   bull_mastiff 0.002569195
## 
## [1] "perro_resnet_0015.png"
## [[1]]
##   class_name              class_description      score
## 1  n02093428 American_Staffordshire_terrier 0.74800014
## 2  n02087394            Rhodesian_ridgeback 0.09011326
## 3  n02093256      Staffordshire_bullterrier 0.07451496
## 4  n02108089                          boxer 0.03279092
## 5  n02109047                     Great_Dane 0.01214166
## 
## [1] "perro_resnet_002.png"
## [[1]]
##   class_name              class_description       score
## 1  n02093428 American_Staffordshire_terrier 0.768633604
## 2  n02093256      Staffordshire_bullterrier 0.111421354
## 3  n02087394            Rhodesian_ridgeback 0.054265834
## 4  n02108089                          boxer 0.050750721
## 5  n02088466                     bloodhound 0.003280226
expls_vgg <- c('flor_vgg16_001.png','flor_vgg16_0015.png','flor_vgg16_002.png','flor_vgg16_003.png',
                'moto_vgg16_001.png','moto_vgg16_0015.png','moto_vgg16_002.png',
                'perro_vgg16_0005.png','perro_vgg16_001.png','perro_vgg16_0015.png','perro_vgg16_002.png','perro_vgg16_003.png')
for (expl in expls_vgg){
  res <- predict(model_vgg16, image_prep(expl))
  pred <- imagenet_decode_predictions(res)
  print(expl)
  print(pred)
}
## [1] "flor_vgg16_001.png"
## [[1]]
##   class_name class_description      score
## 1  n03944341          pinwheel 0.26917592
## 2  n03991062               pot 0.23156908
## 3  n04522168              vase 0.13489044
## 4  n01833805       hummingbird 0.04371330
## 5  n02843684         birdhouse 0.01517356
## 
## [1] "flor_vgg16_0015.png"
## [[1]]
##   class_name class_description      score
## 1  n03991062               pot 0.25171217
## 2  n03944341          pinwheel 0.21441177
## 3  n04522168              vase 0.15763204
## 4  n01833805       hummingbird 0.02829394
## 5  n02843684         birdhouse 0.01404496
## 
## [1] "flor_vgg16_002.png"
## [[1]]
##   class_name class_description      score
## 1  n03944341          pinwheel 0.40724400
## 2  n03991062               pot 0.16375154
## 3  n04522168              vase 0.07807587
## 4  n01833805       hummingbird 0.02749854
## 5  n02843684         birdhouse 0.02396108
## 
## [1] "flor_vgg16_003.png"
## [[1]]
##   class_name class_description      score
## 1  n03991062               pot 0.23978890
## 2  n04522168              vase 0.08391342
## 3  n12267677             acorn 0.06200504
## 4  n01833805       hummingbird 0.03943241
## 5  n01560419            bulbul 0.02918246
## 
## [1] "moto_vgg16_001.png"
## [[1]]
##   class_name class_description      score
## 1  n03785016             moped 0.13955890
## 2  n03127747      crash_helmet 0.08995814
## 3  n03791053     motor_scooter 0.08621930
## 4  n04509417          unicycle 0.06037048
## 5  n04482393          tricycle 0.04572330
## 
## [1] "moto_vgg16_0015.png"
## [[1]]
##   class_name class_description      score
## 1  n03785016             moped 0.24345194
## 2  n03127747      crash_helmet 0.08737680
## 3  n03792782     mountain_bike 0.07551998
## 4  n03791053     motor_scooter 0.07453740
## 5  n04509417          unicycle 0.04126880
## 
## [1] "moto_vgg16_002.png"
## [[1]]
##   class_name class_description      score
## 1  n03903868          pedestal 0.35849673
## 2  n04509417          unicycle 0.26250902
## 3  n02825657         bell_cote 0.08290558
## 4  n03126707             crane 0.02982016
## 5  n03792782     mountain_bike 0.02433574
## 
## [1] "perro_vgg16_0005.png"
## [[1]]
##   class_name              class_description      score
## 1  n02093428 American_Staffordshire_terrier 0.76940113
## 2  n02093256      Staffordshire_bullterrier 0.09453634
## 3  n02107574     Greater_Swiss_Mountain_dog 0.01457082
## 4  n02108089                          boxer 0.01187047
## 5  n02087394            Rhodesian_ridgeback 0.01104719
## 
## [1] "perro_vgg16_001.png"
## [[1]]
##   class_name              class_description      score
## 1  n02093428 American_Staffordshire_terrier 0.71197659
## 2  n02093256      Staffordshire_bullterrier 0.13505511
## 3  n02101388               Brittany_spaniel 0.01619459
## 4  n02091244                   Ibizan_hound 0.01220415
## 5  n02108089                          boxer 0.01136750
## 
## [1] "perro_vgg16_0015.png"
## [[1]]
##   class_name              class_description       score
## 1  n02093428 American_Staffordshire_terrier 0.859513700
## 2  n02093256      Staffordshire_bullterrier 0.119303264
## 3  n02108089                          boxer 0.009755081
## 4  n02087394            Rhodesian_ridgeback 0.003335267
## 5  n02108422                   bull_mastiff 0.002183239
## 
## [1] "perro_vgg16_002.png"
## [[1]]
##   class_name              class_description       score
## 1  n02093428 American_Staffordshire_terrier 0.788916826
## 2  n02093256      Staffordshire_bullterrier 0.180674523
## 3  n02108089                          boxer 0.013026480
## 4  n02087394            Rhodesian_ridgeback 0.004287839
## 5  n02110806                        basenji 0.002374324
## 
## [1] "perro_vgg16_003.png"
## [[1]]
##   class_name              class_description       score
## 1  n02093428 American_Staffordshire_terrier 0.785372317
## 2  n02093256      Staffordshire_bullterrier 0.159573287
## 3  n02108089                          boxer 0.029370276
## 4  n02108422                   bull_mastiff 0.005781381
## 5  n02087394            Rhodesian_ridgeback 0.003781967